package com.csy.rabbit.merchant.service.impl;

import com.csy.rabbit.commit.result.Result;
import com.csy.rabbit.merchant.entity.Merchant;
import com.csy.rabbit.merchant.mapper.MerchantMapper;
import com.csy.rabbit.merchant.service.MerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商家表 服务实现类
 * </p>
 *
 * @author csy
 * @since 2022-12-09
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;
    @Override
    public Result<String> add(Merchant merchant) {
        if(merchant.getOpenId()==null&&merchant.getPhone()==null){
            return Result.error("微信或者手机有空");
        }
        if(merchant.getPassword()==null && merchant.getPhone()==null){
            return Result.error("密码或手机有空");
        }

        int insert = merchantMapper.insert(merchant);
        if(insert==1){
            return Result.success("注册成功");
        }
        return Result.error("注册失败");
    }

    @Override
    public Result<String> findByMerchantId(String merchantId) {
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null) {
        return Result.error("这个用户id不存在");
        }
        //对密码进行空字符串返回
        merchant.setPassword("");
        return Result.success("成功");
    }

    @Override
    public Result<String> deleteById(String merchantId) {
        //脱敏
        Merchant merchant = merchantMapper.selectById(merchantId);
        String phone = merchant.getPhone();
        phone = phone.substring(0,3)+"****"+phone.substring(7);
        merchant.setPhone(phone);
        merchantMapper.updateById(merchant);
        int id = merchantMapper.deleteById(merchantId);
        if(id!=1){
            return Result.error("删除失败");
        }
        return Result.success("成功");
    }
}
